package com.ruoyi.bussiness.service.impl;

import java.util.List;

import com.ruoyi.bussiness.domain.SysBusinessDynamic;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bussiness.mapper.SysBusinessDeptMapper;
import com.ruoyi.bussiness.domain.SysBusinessDept;
import com.ruoyi.bussiness.service.ISysBusinessDeptService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 机构Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-18
 */
@Service
public class SysBusinessDeptServiceImpl implements ISysBusinessDeptService
{
    @Autowired
    private SysBusinessDeptMapper sysBusinessDeptMapper;

    /**
     * 查询机构
     *
     * @param deptId 机构主键
     * @return 机构
     */
    @Override
    public SysBusinessDept selectSysBusinessDeptByDeptId(Long deptId)
    {
        return sysBusinessDeptMapper.selectSysBusinessDeptByDeptId(deptId);
    }

    /**
     * 查询机构列表
     *
     * @param sysBusinessDept 机构
     * @return 机构
     */
    @Override
    public List<SysBusinessDept> selectSysBusinessDeptList(SysBusinessDept sysBusinessDept)
    {
        return sysBusinessDeptMapper.selectSysBusinessDeptList(sysBusinessDept);
    }

    /**
     * 新增机构
     *
     * @param sysBusinessDept 机构
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysBusinessDept(SysBusinessDept sysBusinessDept)
    {
        sysBusinessDept.setCreateTime(DateUtils.getNowDate());
        sysBusinessDept.setIsBottom("0");
        sysBusinessDept.setIsTop("0");

        sysBusinessDeptMapper.insertSysBusinessDept(sysBusinessDept);

        return sysBusinessDeptMapper.updateOrderNum(sysBusinessDept.getDeptId());
    }

    /**
     * 修改机构
     *
     * @param sysBusinessDept 机构
     * @return 结果
     */
    @Override
    public int updateSysBusinessDept(SysBusinessDept sysBusinessDept)
    {
        sysBusinessDept.setUpdateTime(DateUtils.getNowDate());
        return sysBusinessDeptMapper.updateSysBusinessDept(sysBusinessDept);
    }

    /**
     * 批量删除机构
     *
     * @param deptIds 需要删除的机构主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessDeptByDeptIds(Long[] deptIds)
    {
        return sysBusinessDeptMapper.deleteSysBusinessDeptByDeptIds(deptIds);
    }

    /**
     * 删除机构信息
     *
     * @param deptId 机构主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessDeptByDeptId(Long deptId)
    {
        return sysBusinessDeptMapper.deleteSysBusinessDeptByDeptId(deptId);
    }

    @Override
    public AjaxResult moveUpOrDown(SysBusinessDept entity) {
        SysBusinessDept moveEntity = sysBusinessDeptMapper.selectSysBusinessDeptByDeptId(entity.getDeptId());
        if(moveEntity == null){
            return AjaxResult.error("机构不存在，不能上移下移");
        }
        SysBusinessDept moveChangeEntity = null;
        if("up".equalsIgnoreCase(entity.getMoveType())){
            moveChangeEntity = sysBusinessDeptMapper.getLastList(moveEntity.getOrderNum());
            if(moveChangeEntity == null){
                return AjaxResult.error("首行数据不能上移");
            }
            Integer orderNum = moveEntity.getOrderNum();
            moveEntity.setOrderNum(moveChangeEntity.getOrderNum());
            moveChangeEntity.setOrderNum(orderNum);
            sysBusinessDeptMapper.updateSysBusinessDept(moveEntity);
            sysBusinessDeptMapper.updateSysBusinessDept(moveChangeEntity);
        }else if("down".equalsIgnoreCase(entity.getMoveType())){
            moveChangeEntity = sysBusinessDeptMapper.getNextList(moveEntity.getOrderNum());
            if(moveChangeEntity == null){
                return AjaxResult.error("末行数据不能下移");
            }
            Integer orderNum = moveEntity.getOrderNum();
            moveEntity.setOrderNum(moveChangeEntity.getOrderNum());
            moveChangeEntity.setOrderNum(orderNum);
            sysBusinessDeptMapper.updateSysBusinessDept(moveEntity);
            sysBusinessDeptMapper.updateSysBusinessDept(moveChangeEntity);
        }else{
            return AjaxResult.error("无效的移动方式");
        }
        return AjaxResult.success("移动成功",1);
    }
}
